home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / lib / update-notifier / apt_check.py next >
Text File  |  2009-10-15  |  7KB  |  194 lines

  1. #!/usr/bin/python
  2.  
  3.  
  4. #nice apt-get -s -o Debug::NoLocking=true upgrade | grep ^Inst 
  5.  
  6. import apt_pkg
  7. import os
  8. import sys
  9. from optparse import OptionParser
  10. import gettext
  11. import subprocess
  12.  
  13. SYNAPTIC_PINFILE = "/var/lib/synaptic/preferences"
  14. DISTRO = subprocess.Popen(["lsb_release","-c","-s"],
  15.                           stdout=subprocess.PIPE).communicate()[0].strip()
  16.  
  17. class OpNullProgress(object):
  18.     def update(self, percent):
  19.         pass
  20.     def done(self):
  21.         pass
  22.  
  23. def _(msg):
  24.     return gettext.dgettext("update-notifier", msg)
  25.  
  26. def _handleException(type, value, tb):
  27.     sys.stderr.write("E: "+ _("Unknown Error: '%s' (%s)") % (type,value))
  28.     sys.exit(-1)
  29.  
  30. def clean(cache,depcache):
  31.     " unmark (clean) all changes from the given depcache "
  32.     # mvo: looping is too inefficient with the new auto-mark code
  33.     #for pkg in cache.Packages:
  34.     #    depcache.MarkKeep(pkg)
  35.     depcache.Init()
  36.  
  37. def saveDistUpgrade(cache,depcache):
  38.     """ this functions mimics a upgrade but will never remove anything """
  39.     depcache.Upgrade(True)
  40.     if depcache.DelCount > 0:
  41.         clean(cache,depcache)
  42.     depcache.Upgrade()
  43.  
  44. def isSecurityUpgrade(ver):
  45.     " check if the given version is a security update (or masks one) "
  46.     security_pocket = "%s-security" % DISTRO
  47.     for (file, index) in ver.FileList:
  48.         if (file.Archive == security_pocket and file.Origin == "Ubuntu"):
  49.             return True
  50.     return False
  51.  
  52. def write_package_names(outstream, cache, depcache):
  53.     " write out package names that change to outstream "
  54.     pkgs = filter(lambda pkg:
  55.                   depcache.MarkedInstall(pkg) or depcache.MarkedUpgrade(pkg),
  56.                   cache.Packages)
  57.     outstream.write("\n".join(map(lambda p: p.Name, pkgs)))
  58.  
  59. def write_human_readable_summary(outstream, upgrades, security_updates):
  60.     " write out human summary summary to outstream "
  61.     outstream.write(gettext.dngettext("update-notifier",
  62.                             "%i package can be updated.",
  63.                             "%i packages can be updated.",
  64.                             upgrades) % upgrades)
  65.     outstream.write("\n")
  66.     outstream.write(gettext.dngettext("update-notifier",
  67.                             "%i update is a security update.",
  68.                             "%i updates are security updates.",
  69.                             security_updates)  % security_updates)
  70.     outstream.write("\n")
  71. def init():
  72.     " init the system, be nice "
  73.     # FIXME: do a ionice here too?
  74.     os.nice(19)
  75.     apt_pkg.init()
  76.     # force apt to build its caches in memory for now to make sure
  77.     # that there is no race when the pkgcache file gets re-generated
  78.     apt_pkg.Config.Set("Dir::Cache::pkgcache","")
  79.     
  80. def run(options=None):
  81.  
  82.     # we are run in "are security updates installed automatically?"
  83.     # question mode
  84.     if options.security_updates_unattended:
  85.         res = apt_pkg.Config.FindI("APT::Periodic::Unattended-Upgrade", 0)
  86.         #print res
  87.         sys.exit(res)
  88.  
  89.     # get caches
  90.     try:
  91.         cache = apt_pkg.GetCache(OpNullProgress())
  92.     except SystemError, e:
  93.         sys.stderr.write("E: "+ _("Error: Opening the cache (%s)") % e)
  94.         sys.exit(-1)
  95.     depcache = apt_pkg.GetDepCache(cache)
  96.  
  97.     # read the pin files
  98.     depcache.ReadPinFile()
  99.     # read the synaptic pins too
  100.     if os.path.exists(SYNAPTIC_PINFILE):
  101.         depcache.ReadPinFile(SYNAPTIC_PINFILE)
  102.  
  103.     # init the depcache
  104.     depcache.Init()
  105.  
  106.     if depcache.BrokenCount > 0:
  107.         sys.stderr.write("E: "+ _("Error: BrokenCount > 0"))
  108.         sys.exit(-1)
  109.  
  110.     # do the upgrade (not dist-upgrade!)
  111.     try:
  112.         saveDistUpgrade(cache,depcache)
  113.     except SystemError, e:
  114.         sys.stderr.write("E: "+ _("Error: Marking the upgrade (%s)") % e)
  115.         sys.exit(-1)
  116.  
  117.     # analyze the ugprade
  118.     upgrades = 0
  119.     security_updates = 0
  120.     for pkg in cache.Packages:
  121.         # skip packages that are not marked upgraded/installed
  122.         if not (depcache.MarkedInstall(pkg) or depcache.MarkedUpgrade(pkg)):
  123.             continue
  124.         # check if this is really a upgrade or a false positive
  125.         # (workaround for ubuntu #7907)
  126.         inst_ver = pkg.CurrentVer
  127.         cand_ver = depcache.GetCandidateVer(pkg)
  128.         if cand_ver == inst_ver:
  129.             continue
  130.  
  131.         # check for security upgrades
  132.         upgrades = upgrades + 1    
  133.         if isSecurityUpgrade(cand_ver):
  134.             security_updates += 1
  135.             continue
  136.  
  137.         # now check for security updates that are masked by a 
  138.         # canidate version from another repo (-proposed or -updates)
  139.         for ver in pkg.VersionList:
  140.             if (inst_ver and apt_pkg.VersionCompare(ver.VerStr, inst_ver.VerStr) <= 0):
  141.                 #print "skipping '%s' " % ver.VerStr
  142.                 continue
  143.             if isSecurityUpgrade(ver):
  144.                 security_updates += 1
  145.                 break
  146.  
  147.     # print the number of upgrades
  148.     if options and options.show_package_names:
  149.         write_package_names(sys.stderr, cache, depcache)
  150.     elif options and options.readable_output:
  151.         write_human_readable_summary(sys.stdout, upgrades, security_updates)
  152.     else:
  153.         # print the number of regular upgrades and the number of 
  154.         # security upgrades
  155.         sys.stderr.write("%s;%s" % (upgrades,security_updates))
  156.  
  157.     # return the number of upgrades (if its used as a module)
  158.     return(upgrades,security_updates)
  159.  
  160.  
  161. if __name__ == "__main__":        
  162.     # setup a exception handler to make sure that uncaught stuff goes
  163.     # to the notifier
  164.     sys.excepthook = _handleException
  165.     
  166.     # gettext
  167.     APP="update-notifier"
  168.     DIR="/usr/share/locale"
  169.     gettext.bindtextdomain(APP, DIR)
  170.     gettext.textdomain(APP)
  171.  
  172.     # check arguments
  173.     parser = OptionParser()
  174.     parser.add_option("-p",
  175.                       "--package-names",
  176.                       action="store_true",
  177.                       dest="show_package_names",
  178.                       help=_("Show the packages that are going to be installed/upgraded"))
  179.     parser.add_option("",
  180.                       "--human-readable",
  181.                       action="store_true",
  182.                       dest="readable_output",
  183.                       help=_("Show human readable output on stdout"))
  184.     parser.add_option("",
  185.                       "--security-updates-unattended",
  186.                       action="store_true",
  187.                       help=_("Return the time in days when security updates "
  188.                              "are installed unattended (0 means disabled)"))
  189.     (options, args) = parser.parse_args()
  190.  
  191.     # run it
  192.     init()
  193.     run(options)
  194.